package Thread;


/**
 * 生产者消费者模型
 */

public class Demo31 {
    public static void main(String[] args) {
        //先创建一个阻塞队列
        MyBlockingQueue queue = new MyBlockingQueue(1000);

        //生产者线程
        Thread t1 = new Thread(()-> {
            int i = 1;
            while(true){
                try {
                    queue.put("" + 1);
                    System.out.println("生产元素" + i);
                    i ++;

                    //给生产操作加上sleep,生产慢点,消费快点
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        });

        //消费者线程
        Thread t2 = new Thread(()->{
            while(true){
                Integer i = null;
                try {
                    i = Integer.parseInt(queue.take());
                    System.out.println("消费元素 " + i);

                    // 给消费操作, 加上 sleep, 生产快点, 消费慢点
                    // Thread.sleep(1000);

                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }

            }
        });

        //启动线程
        t1.start();
        t2.start();
    }
}
